<?php

namespace App\Http\Middleware;

use App\Services\UserInfoJwt;
use Closure;
use Illuminate\Http\Request;

class UserInfoMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        try {
            $token = $request->header('token');
            $jwt = (new UserInfoJwt())->jwtDe($token);
            $request->user_id = $jwt['token'];
            return $next($request);
        } catch (\Exception $exception) {
            return response(['code' => 9000, 'mes' => '请登陆', 'data' => []]);
        }
    }
}